package com.ysd.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.UserinfoDaoimpl;
import com.ysd.entity.Userinfo;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminLiuYanServlet extends BaseServlet {

	/**
	 * localhost:8080/xiyangyangchaoshi/adminliuyan?reqName=selLiuYan
	  *   查询留言的数据
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selLiuYan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserinfoDaoimpl userDao=new UserinfoDaoimpl();
		
		int page=Integer.parseInt(request.getParameter("page"));
		int size=Integer.parseInt(request.getParameter("size"));
		
		ArrayList<Userinfo> liuYan= userDao.getLiuYan(page,size);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(liuYan));
		
		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminliuyan?reqName=selLiuYanSize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selLiuYanSize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserinfoDaoimpl userDao=new UserinfoDaoimpl();
		
		
		
		ArrayList<Userinfo> liuYan= userDao.getLiuYanSize();
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		Gson gson=new Gson();
		out.print(gson.toJson(liuYan.size()));
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminliuyan?reqName=delLiuYan
	 * 	根据id删除李艳
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delLiuYan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserinfoDaoimpl userDao=new UserinfoDaoimpl();
		int liuYanId=Integer.parseInt(request.getParameter("liuYanId"));
		int n= userDao.delLiuYan(liuYanId);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		if(n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		}else {
			out.print(Result.toClient(true, "删除失败", "您的参数有误"));
		}
		out.flush();
		out.close();
	}
	
	
	
	/**
	 * localhost:8080/xiyangyangchaoshi/adminliuyan?reqName=liuYan_edit
	 * 修改已读未读
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void liuYan_edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserinfoDaoimpl userDao=new UserinfoDaoimpl();
		
		int userId=Integer.parseInt(request.getParameter("id"));
		String zhuangtai=request.getParameter("zhuangtai");
		Userinfo Userinfo=new Userinfo(userId, null, null, null, null, null, zhuangtai);
		int n= userDao.liuYan_edit(Userinfo);
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		if(n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		}else {
			out.print(Result.toClient(true, "修改失败", "您的参数有误"));
		}
		out.flush();
		out.close();
	}
}
